﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mengsk.ERP.Database;

namespace Mengsk.ERP.App.ArrvieInDelivery
{
    /// <summary>
    /// ArriveInQueryUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class ArriveInQueryUserControl : UserControl, IUIPlugin
    {
        public ArriveInQueryUserControl()
        {
            InitializeComponent();
        }


        public string Title
        {
            get { return "到货物流单号查询"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                StringBuilder where = new StringBuilder();
                bool appendAnd = false;
                string deliveryCompany = this.cbbDeliveryCompany.Text.Trim();
                string deliveryNumber = this.tbDeliveryNumber.Text.Trim();
                string arriveInTime = this.dpArriveInTime.SelectedDate == null ? "" : this.dpArriveInTime.SelectedDate.Value.ToString("yyyy-MM-dd");
                string comment = this.tbComment.Text.Trim();
                string arriveReason = this.cbbReason.Text.Trim();

                if (string.IsNullOrWhiteSpace(deliveryNumber) == false)
                {
                    if (appendAnd)
                    {
                        where.Append(" and ");
                    }
                    where.Append(string.Format("[DeliveryNumber] like '%{0}%'", deliveryNumber));
                    appendAnd = true;
                }

                if (string.IsNullOrWhiteSpace(deliveryCompany) == false && deliveryCompany!="所有")
                {
                    if (appendAnd)
                    {
                        where.Append(" and ");
                    }
                    where.Append(string.Format("[deliveryCompany] like '%{0}%'", deliveryCompany));
                    appendAnd = true;
                }

                if (string.IsNullOrWhiteSpace(comment) == false)
                {
                    if (appendAnd)
                    {
                        where.Append(" and ");
                    }
                    where.Append(string.Format("[comment]='{0}'", comment));
                    appendAnd = true;
                }

                if (string.IsNullOrWhiteSpace(arriveReason) == false && arriveReason != "所有")
                {
                    if (appendAnd)
                    {
                        where.Append(" and ");
                    }
                    where.Append(string.Format("[ArriveReason]='{0}'", arriveReason));
                    appendAnd = true;
                }

                if (string.IsNullOrWhiteSpace(arriveInTime) == false)
                {
                    if (appendAnd)
                    {
                        where.Append(" and ");
                    }
                    where.Append(string.Format("CAST ([CreateTime] as date) ='{0}'", arriveInTime));
                    appendAnd = true;
                }

                var items = DBHelper.Instance.Select<ArriveInDeliveryItem>("", "ArrvieInDelivery", where.ToString(), null, false);
                this.dgvData.ItemsSource = items;
                this.tbTotal.Text = string.Format("当前共:{0} 条记录", items.Length);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
